package cn.zwx.business.member.service.remote;

import cn.zwx.business.order.api.req.RpcRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author zhangwenxue
 */
@Component
public class RemoteInvocationHandler implements InvocationHandler {

    @Value("${zwx.rpc.server.host:localhost}")
    private String host;
    @Value("${zwx.rpc.server.port:8006}")
    private int port;


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        // 进行远程调用
        RpcNetTransport rpcNetTransport = new RpcNetTransport(host, port);
        RpcRequest rpcRequest = new RpcRequest();
        // 参数
        rpcRequest.setArgs(args);
        // 类名
        rpcRequest.setClassName(method.getDeclaringClass().getName());
        // 方法名称
        rpcRequest.setMethodName(method.getName());
        // 方法参数类型
        rpcRequest.setType(method.getParameterTypes());
        return rpcNetTransport.send(rpcRequest);
    }
}
